/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function loadXMLDOC(dname){
    if(window.XMLHttpRequest){
        xhttp = new XMLHttpRequest();
    }else{
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET", dname, false);
    xhttp.send("");
    return xhttp.responseXML;
}
var dom1 ;
var dom2 ;
var dom3 ;

function search(search){
    search = search.replace(/ +(?= )/g,'');
    var d = toNoneUniCode(search.toLowerCase());
    if(dom1== null){
//        alert("search lan 1");
        dom1 = loadXMLDOC("xml/FPTShop.xml");
    }
    searchInXML(d, dom1,"r1", "img/logoFPT.png");
    if(dom2== null){
        dom2 = loadXMLDOC("xml/dienmay.xml");
    }
    searchInXML(d,dom2, "r2", "img/logo-dien-may.png");
    if(dom3== null){
        dom3 = loadXMLDOC("xml/bkcXpath.xml");
    }
    searchInXML(d, dom3, "r3", "img/bkc.png");

}

function searchInXML(search, domFile, divID, img){
    document.getElementById("table").innerHTML = "";
    document.getElementById(divID).innerHTML = "<center><img src='"+img+"' witdh=80px height=60px /></center>";
          

    var path = "//product/name";
    //alert("vo duoc ham search roi");
    if(document.implementation && document.implementation.createDocument){
//        alert("qua if    "+ domFile);
        var nodes = domFile.evaluate(path, domFile, null, XPathResult.ANY_TYPE, null);
        alert(nodes.toString());
        var array = new Array();
        var result = nodes.iterateNext();
        
        while(result){
            var str = toNoneUniCode(result.childNodes[0].nodeValue.toLowerCase());

            var currentNameValue =result.childNodes[0].nodeValue;
          
            if(str.match(search)){
                var xpathResult = "//product[name ='"+currentNameValue+"']";
                // alert(xpathResult);
                if(document.implementation && document.implementation.createDocument){
                    var nodesResult = domFile.evaluate(xpathResult, domFile, null, XPathResult.ANY_TYPE, null);
                    rs = nodesResult.iterateNext();
                    while(rs){
                        array[0] = rs.getElementsByTagName("name")[0].childNodes[0].nodeValue;
                        array[1] = rs.getElementsByTagName("price")[0].childNodes[0].nodeValue;
                        array[2] = rs.getElementsByTagName("imgLink")[0].childNodes[0].nodeValue;
                        array[3] = rs.getElementsByTagName("productLink")[0].childNodes[0].nodeValue;
                        addDiv(array,divID);
                        rs = nodesResult.iterateNext();
                    }
                }
            }
            result = nodes.iterateNext();
        }
    }
}
function addDiv(array, divID){

    var div = document.createElement("div");
  div.setAttribute("class", "searchClass");
    
    div.innerHTML = "<center><h2>"+array[0].substring(0,25)+"...</h2></center><br/>"+"<center><img src='"+array[2]+"' witdh=260px height=260px /></center><center><h2 style='color: red;'>Giá:"+array[1]+"</h2></center><br/><br/>";
    var pa = document.getElementById(divID);
    pa.appendChild(div);
}
function toNoneUniCode(str){
    var st= new Array();
    //    alert(str);
    var strReplace = new Array("a","d","e","i","o","u","y");
    st[0]=new Array("à","á","ả","ã","ạ","â","ấ","ầ","ẩ","ẫ","ậ","ắ","ằ");
    st[1]=new Array("đ");
    st[2]=new Array("è","é","ẻ","ẽ","ẹ","ê","ề","ế","ể","ễ","ệ");
    st[3]=new Array("ì","í","ỉ","ĩ","ị");
    st[4]=new Array("ò","ó","ỏ","õ","ọ","ô","ồ","ố","ổ","ỗ","ộ","ơ","ờ","ớ","ở","ỡ","ợ");
    st[5]=new Array("ù","ú","ủ","ũ","ụ","ư","ừ","ứ","ử","ữ","ự");
    st[6]=new Array("ỳ","ý","ỷ","ỹ","ỵ");
    for(i=0;i<=st.length-1; i++ ){
        for(j=0; j<=st[i].length-1; j++){
            str = str.replace(st[i][j], strReplace[i]);
        }
    }
    return str;
}
function slipToArraỵ(str){
    var resSult = str.split(" ");
    return resSult;
}
